package com.ambientrs.bot;

import com.ambientrs.script.methods.Calculations;

import java.awt.*;
import java.util.ArrayList;

public class InputManager {

    private int x = 0;
    private int y = 0;

    public void moveMouse(Point p) {
        ArrayList<Point> points = new ArrayList<Point>();
        int x4 = p.x;
        int y4 = p.y;
        int x2 = (x4 + x) / 3;
        int y2 = (y4 + y) / 3;
        int x3 = x2 * 2 + Calculations.random(0, Math.abs(p.x - x) / 3);
        int y3 = y2 * 2 + Calculations.random(0, Math.abs(p.y - y) / 3);
        x3 += Calculations.random(0, Math.abs(p.x - x) / 3);
        y3 += Calculations.random(0, Math.abs(p.y - y) / 3);
        double[] ta = {0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6,
                0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0};
        for (double t : ta) {
            int cx = 3 * (x2 - x);
            int bx = 3 * (x3 - x2) - cx;
            int ax = x4 - x - cx - bx;
            int xn = (int) (ax * Math.pow(t, 3) + bx * Math.pow(t, 2) + cx * t + x);
            int cy = 3 * (y2 - y);
            int by = 3 * (y3 - y2) - cy;
            int ay = y4 - y - cy - by;
            int yn = (int) (ay * Math.pow(t, 3) + by * Math.pow(t, 2) + cy * t + y);
            points.add(new Point(xn, yn));
        }
        //TODO add code to change x & y (move mouse)

    }

    public void hopMouse(Point p) {
        x = p.x;
        y = p.y;
    }

    /*public void generateBezier(int num, Point p) throws IOException {
        for (int k = 1; k <= num; k++) {
            ArrayList<Point> points = new ArrayList<Point>();
            int x4 = p.x;
            int y4 = p.y;
            int x2 = (x4 + x) / 3;
            int y2 = (y4 + y) / 3;
            int x3 = x2 * 2 + Calculations.random(0, Math.abs(p.x - x) / 3);
            int y3 = y2 * 2 + Calculations.random(0, Math.abs(p.y - y) / 3);
            x3 += Calculations.random(0, Math.abs(p.x - x) / 3);
            y3 += Calculations.random(0, Math.abs(p.y - y) / 3);
            double[] ta = {0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6,
                    0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0};
            for (double t : ta) {
                int cx = 3 * (x2 - x);
                int bx = 3 * (x3 - x2) - cx;
                int ax = x4 - x - cx - bx;
                int xn = (int) (ax * Math.pow(t, 3) + bx * Math.pow(t, 2) + cx * t + x);
                int cy = 3 * (y2 - y);
                int by = 3 * (y3 - y2) - cy;
                int ay = y4 - y - cy - by;
                int yn = (int) (ay * Math.pow(t, 3) + by * Math.pow(t, 2) + cy * t + y);
                points.add(new Point(xn, yn));
            }

            BufferedImage bi = new BufferedImage(700, 700, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            g.setColor(Color.BLACK);
            g.setStroke(new BasicStroke(3f, BasicStroke.JOIN_ROUND, BasicStroke.CAP_ROUND));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            for (int i = 1; i < points.size(); i++) {
                Point a = points.get(i - 1);
                Point b = points.get(i);
                g.drawLine(a.x + 100, a.y + 100, b.x + 100, b.y + 100);
            }
            ImageIO.write(bi, "png", new File("bezier/" + k + ".png"));
        }
    }     */
}
